//
//  MMRefreshView.m
//  zhihuDaily
//
//  Created by 赖锦浩 on 16/6/12.
//  Copyright © 2016年 cc. All rights reserved.
//

#import "MMRefreshView.h"

@interface MMRefreshView ()

/** scrollView  */
@property (nonatomic, strong) UIScrollView *scrollView;

/** 刷新标识  */
@property (nonatomic, assign) CGFloat refreshFalg;

/** 活动指示器  */
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;

/** 形状图层(进度)  */
@property (nonatomic,weak) CAShapeLayer *progressLayer;

/** 是否刷新  */
@property (nonatomic,assign,getter=isRefreshing) BOOL refreshing;

@end

@implementation MMRefreshView


+ (instancetype)refreshWithScrollView:(UIScrollView *)scrollView
{
    //当前控制器的View
    MMRefreshView *refreshView = [[self alloc] init];
    refreshView.frame = CGRectMake(0, 0, 18, 18);
    refreshView.scrollView = scrollView;
    //refreshView.backgroundColor = [UIColor redColor];
    
    //进度
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    //添加到refreshView的layer层
    [refreshView.layer addSublayer:progressLayer];
    progressLayer.strokeColor = [UIColor whiteColor].CGColor;
    progressLayer.backgroundColor = [UIColor clearColor].CGColor;
    progressLayer.fillColor = [UIColor clearColor].CGColor;
    progressLayer.strokeEnd = 0.0;
    progressLayer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 0, 1);
    progressLayer.lineWidth = 2.0;
    refreshView.progressLayer = progressLayer;
    
    //活动指示器
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] init];
    indicatorView.frame = CGRectMake(0, 0, 18, 18);
    refreshView.indicatorView = indicatorView;
   // [indicatorView stopAnimating];
    [refreshView addSubview:indicatorView];
    
    //添加通知 监听值改变
    [refreshView.scrollView addObserver:refreshView forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];

   
    return refreshView;
}

- (void)layoutSublayersOfLayer:(CALayer *)layer
{
    [super layoutSublayersOfLayer:layer];
    self.progressLayer.frame = self.bounds;
    self.progressLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, 2, 2)].CGPath;

}


- (void)layoutSubviews
{
//    [super layoutSubviews];
    self.indicatorView.frame = self.bounds;
}



- (void)dealloc
{
    //控制器销毁的时候移除通知
    [self.scrollView removeObserver:self forKeyPath:@"contentOffset"];

}
/** 根据kvo监听的对象的值改变执行处理 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    //如果正在刷新,直接返回
    if (self.isRefreshing) return;


    //height 就是 新值改变的y值的大小
    CGFloat height = [change[@"new"] CGPointValue].y;
    
    if (height > 0) {
        height = 0;
    }else if (height <= -80)
    {
        height = 1.0;
    }else
    {
        height = height / - 80;
    }

    //scrollView是否拖动
    if (self.scrollView.isDragging) { //正在拖动
        
        self.hidden = NO;
        self.refreshFalg = height;
        self.progressLayer.hidden = NO;
        self.progressLayer.strokeEnd = height;
        [self.indicatorView stopAnimating];
        
    }else
    {
        if (self.refreshFalg == 1) { //完成刷新
            self.refreshing = 0;
            self.refreshing = YES;
            [self.indicatorView startAnimating];
            self.progressLayer.hidden = YES;
            self.progressLayer.strokeEnd = 0;

            
        }else
        { //停止拖动但是没有完成刷新
            
            [self.indicatorView stopAnimating];
            self.progressLayer.hidden = NO;
            self.progressLayer.strokeEnd = height;
        }
 
    }
    
}

/** 结束刷新 */
- (BOOL)endRefresh
{
    //延时操作  看效果
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.hidden = YES;
        self.refreshing = NO;
        self.progressLayer.strokeEnd = 0;
        self.progressLayer.hidden = NO;

        [self.indicatorView stopAnimating];

    });
    
    return YES;
}
@end
